<?php 
	header('content-type:text/html;charset=utf-8');

	/**
	 * 页面自动刷新
	 */
	echo ("<script type=\"text/javascript\">");
	echo ("function fresh_page()");    
	echo ("{");
	echo ("window.location.reload();");
	echo ("}"); 
	echo ("setTimeout('fresh_page()',5000);");//自动刷新时间 毫秒为单位、  
	echo ("</script>");

	/**
	 * 数据库配置信息
	 */
	$dbhost = 'localhost:3306';  // mysql服务器主机地址
	$dbuser = 'root';            // mysql用户名
	$dbpass = 'root';          // mysql用户名密码
	$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
	if(!$conn )
	{
	    die('Could not connect: ' . mysqli_error());
	}
	/**
	 * 选择数据库 mysqli_select_db();
	 * 选择字符编码 mysqli_query();
	 */
	mysqli_select_db($conn, 'test' );
	mysqli_query($conn , "set names utf8");
	$sql = " select qihao from mgk3 order by time desc limit 1 ";
	$qihao = @mysqli_fetch_assoc(mysqli_query($conn,$sql)); 
	$arr = $qihao['qihao'];
	//print_r($qihao['qihao']);die;

	/**
	 * [抓包Json数据]
	 */
	$url = 'http://nmgk3.zhcvideo.com/Echart/k3/he?flag=49';
	$html = file_get_contents($url);
	$data = json_decode($html);
	$qihao = $data->table[0]->qihao;
	$q1 = $data->table[0]->q1;
	$q2 = $data->table[0]->q2;
	$q3 = $data->table[0]->q3;
	$kuadu = $data->table[0]->kuadu;
	$hezhi = $data->table[0]->hezhi;
	echo "接受最新数据文本";
	echo "<hr/>";
	echo "最新更新期号:". $qihao;
	echo "<br/>";
	echo "最新期号开奖码:". $q1,$q2,$q3;
	echo "<hr/>";
	echo "本地更新期号:".$arr;
	echo "<pre>";

	/**
	 * 判断数据库与当前一期是否一样
	 * 不一样则存入数据库
	 */
		if($qihao !== $arr){	
			$sql = "insert into mgk3 
					(qihao,q1,q2,q3,kuadu,hezhi,time) 
					values 
					('$qihao','$q1','$q2','$q3','$kuadu','$hezhi',NOW())";  
			$query = mysqli_query($conn,$sql);
			echo "更新一条新数据";
		}else{
			echo "请等待数据更新";
		}
	//关闭数据库
	mysqli_close($conn);
 ?>